package com.appkubes.mall.admin.application.message.controller;


import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.base.BaseController;
import com.appkubes.mall.admin.application.message.service.MessageService;
import com.appkubes.mall.admin.utils.PageUtil;
import com.appkubes.mall.core.po.MallMessage;
import com.appkubes.mall.core.po.MallMessageWithBLOBs;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api("文章模块")
@RestController
@RequestMapping("/sass_admin/Smessage")
public class MessageController extends BaseController {
    @Autowired
    MessageService messageService;

    @ApiOperation(value = "文章列表")
    @PostMapping("/getList")
    public Response getList(@RequestParam(defaultValue = "") String key,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer limit,
                            @RequestParam(defaultValue = "message_key") String sort,
                            @RequestParam(defaultValue = "desc") String order) {

        PageInfo<MallMessage> pageinfo = messageService.querySelective(page, limit, sort, order);
        for (MallMessage model : pageinfo.getList()) {
        }
        return Response.success(new PageUtil<MallMessage>(pageinfo));
    }

    @ApiOperation(value = "添加文章")
    @PostMapping("/save")
    public Object save(MallMessageWithBLOBs model) {
        int result = 0;
        if (!StringUtils.isEmpty(model.getMessageKey())) {
            result = messageService.update(model);
        } else {
            result = messageService.add(model);
        }
        if (result > 0) {
            return Response.success(result);
        } else {
            return Response.error("保存失败");
        }
    }

    @ApiOperation(value = "修改文章")
    @PostMapping("/edit")
    public Object edit(@RequestParam String messageKey) {
        MallMessage model = messageService.findById(messageKey);
        return Response.success(model);
    }

    @ApiOperation(value = "删除文章")
    @PostMapping("/delete")
    public Response delete(@RequestParam String ids) {

        int result = messageService.deleteByIds(ids.split(","));
        if (result > 0) {
            return Response.success(result);
        }
        return Response.error("删除失败");

    }

    @ApiOperation(value = "启用文章")
    @PostMapping("/batchenable")
    public Response batchenable(@RequestParam String ids) {
        int ret = messageService.batchenable(ids.split(","));
        if (ret > 0) {
            return Response.success("取消成功");
        } else {
            return Response.error("取消失败");
        }
    }

    @ApiOperation(value = "禁用文章")
    @PostMapping("/batchunenable")
    public Response batchunenable(@RequestParam String ids) {
        int ret = messageService.batchunenable(ids.split(","));
        if (ret > 0) {
            return Response.success("禁用成功");
        } else {
            return Response.error("禁用失败");
        }
    }

}
